package com.github.rxyor.netty.exemple.case2;

import com.github.rxyor.netty.exemple.Config.Server;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
 *<p>
 *
 *</p>
 *
 * @author liuyang
 * @date 2020/5/23 周六 17:12:00
 * @since 1.0.0
 */
public class ChatClient {

    public static void main(String[] args) throws Exception {
        ChatClient client = new ChatClient();
        client.startup();
    }

    @SuppressWarnings("all")
    private void startup() throws Exception {
        EventLoopGroup client = new NioEventLoopGroup();

        try {
            Bootstrap bootstrap = new Bootstrap();
            bootstrap.group(client)
                .channel(NioSocketChannel.class)
                .handler(new ChatClientInitializer());

            ChannelFuture future = bootstrap.connect(Server.ADDRESS, Server.PORT).sync();
            Channel channel = future.channel();

            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
            for (; ; ) {
                channel.writeAndFlush(reader.readLine() + "\r\n");
            }
        } finally {
            client.shutdownGracefully();
        }
    }
}
